Here is the entire VRML 2.0 source code. Type (or paste) this code into a file called "musical.wrl".
#VRML V2.0 utf8 #Example of the use and control of sound #Sphere which will act as an on/off switch DEF SPHERE Transform { translation 0 0 0 children [ DEF CLICKER TouchSensor {} Shape { appearance Appearance { material Material {diffuseColor 0 0 1} } geometry Sphere {radius 3} } ] } #Script to control the music DEF MUSIC_SCRIPT Script { url "musical.class" scriptType "javabc" field SFNode sphere_trans USE SPHERE eventOut SFTime musicOn eventOut SFTime musicOff eventIn SFBool clicked } #sound node DEF MUSICALSOUND Sound { location 0 0 0 #inner radius - intensity is max within this minBack 20 minFront 20 #outer radius - sound drops off between inner #radius and this, and can't be heard beyond this maxBack 50 maxFront 50 intensity 1 source DEF MUSICALCLIP AudioClip { url "musical.wav" loop TRUE stopTime 1 } } #route for mouse click ROUTE CLICKER.isActive TO MUSIC_SCRIPT.clicked #routes for music control ROUTE MUSIC_SCRIPT.musicOn TO MUSICALCLIP.set_startTime ROUTE MUSIC_SCRIPT.musicOff TO MUSICALCLIP.set_stopTime
Here is the Java source code. Type this into a file called "musical.java".
/* musical.java : source for sound tutorial Written By : Jai Natarajan Sony Pictures Imageworks */ import vrml.*; import vs.*; public class musical extends Script { // Grab the eventOuts for music control SFTime musicOn = (SFTime)getEventOut("musicOn"); SFTime musicOff = (SFTime)getEventOut("musicOff"); //current status of the sound boolean musicPlaying = false; public musical() { } public void clicked(ConstSFBool ev, ConstSFTime time) { if(ev.getValue() == true) { return; } else { // get time of click (most recent one we know of) double now = time.getValue(); if(!musicPlaying) { // we need to switch it on // Set the startTime to the latest time musicOn.setValue(now); } else { // switch it off // Set the stopTime to latest time musicOff.setValue(now); } musicPlaying = !musicPlaying; } } }